package com.upf1sh.service;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

@Service
public class ThingsboardTelemetryService {
    //    private String apiUrl;
//    private String entityId;
//    private String accessToken = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI3MTAzMjA1MzRAcXEuY29tIiwic2NvcGVzIjpbIlRFTkFOVF9BRE1JTiJdLCJ1c2VySWQiOiIyYjM0Zjc5MC1mMzU2LTExZWEtODAyNy02M2ZjMjA5MzIzMzAiLCJlbmFibGVkIjp0cnVlLCJpc1B1YmxpYyI6ZmFsc2UsInRlbmFudElkIjoiMWQ5ZDBjMzAtZjM1Ni0xMWVhLTgwMjctNjNmYzIwOTMyMzMwIiwiY3VzdG9tZXJJZCI6IjEzODE0MDAwLTFkZDItMTFiMi04MDgwLTgwODA4MDgwODA4MCIsImlzcyI6InRoaW5nc2JvYXJkLmlvIiwiaWF0IjoxNjg5MTQwMzQ3LCJleHAiOjE2ODkxNDkzNDd9.NR_Pjr0gBOMnz0tIDWdFyfEsk1fV4eJa8jhmLaFkbHvC_rJMfrZ98NM_Qi_T8Uft8fDeWuhyARWUA1SDQQ_NWQ";
    private String accessToken = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI3MTAzMjA1MzRAcXEuY29tIiwic2NvcGVzIjpbIlRFTkFOVF9BRE1JTiJdLCJ1c2VySWQiOiIyYjM0Zjc5MC1mMzU2LTExZWEtODAyNy02M2ZjMjA5MzIzMzAiLCJlbmFibGVkIjp0cnVlLCJpc1B1YmxpYyI6ZmFsc2UsInRlbmFudElkIjoiMWQ5ZDBjMzAtZjM1Ni0xMWVhLTgwMjctNjNmYzIwOTMyMzMwIiwiY3VzdG9tZXJJZCI6IjEzODE0MDAwLTFkZDItMTFiMi04MDgwLTgwODA4MDgwODA4MCIsImlzcyI6InRoaW5nc2JvYXJkLmlvIiwiaWF0IjoxNjkzMzgyNjYzLCJleHAiOjE2OTMzOTE2NjN9.qE3R97PjVEDJBKOf0O2J6VCSE3t9UJhfE90fXQdr79u-SG835ux-sfLyeiZFoCf0QeY9Vkl7EQV6S376fc2-JA";

    public ThingsboardTelemetryService() {

    }

    public void getTelemetryData(String entityId) throws IOException {
        // Create the URL object
        String endpointUrl = "http://106.12.118.231:1088/api/plugins/telemetry/DEVICE/{entityId}/values/timeseries".replace("{entityId}", entityId);
        URL url = new URL(endpointUrl);
        // Create an HTTP client
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            // Create an HTTP GET request with authorization header
            HttpGet request = new HttpGet(url.toString());
            request.addHeader("X-Authorization", "Bearer " + accessToken);
            // Send the GET request
            HttpResponse response = client.execute(request);
            // Get the response code
            int responseCode = response.getStatusLine().getStatusCode();
            // If the response code indicates success
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // Read the response payload
                HttpEntity entity = response.getEntity();
                String responseBody = EntityUtils.toString(entity);
                // Print the received telemetry data
                System.out.println("Telemetry Data: " + responseBody);
            } else {
                // Print the error response
                System.out.println("Error response: " + response.getStatusLine().getReasonPhrase());
            }

        }
    }
}
